const response = require("../../utils/response.js");
const validate = require("../../utils/validate.js");
const categoryService = require("../services/categoryService.js");
const { URLSearchParams } = require('url');
const { createCategoryRules, deleteCategoryRules, updateCategoryRules, getCategoryRules } = require("../validations/categoryValidation.js");

class CategoryController {
    async addCategory(ctx) {
        try {
            const categoryData = await validate(ctx, createCategoryRules);

            const  { id } = await categoryService.addCategory(categoryData);
            response.success(ctx, id, '添加成功');
        }catch(err) {
            console.log(err);
            response.error(ctx, err);
        }
    }
    async deleteCategory(ctx){
        try{
            const categoryData = await validate(ctx, deleteCategoryRules);
            const affectedRows = await categoryService.deleteCategory(categoryData.id);
            if(affectedRows > 0){
                response.success(ctx, categoryData.id, '删除成功')
            }else{
                response.error(ctx, '删除失败')
            }
        }catch(err){
            console.log(err)
            response.error(ctx, err);
        }
    }
    async updateCategory(ctx){
        try{
            const categoryData = await validate(ctx, updateCategoryRules);
            const affectedRows = await categoryService.updateCategory(categoryData);
            if(affectedRows > 0){
                response.success(ctx, '修改成功')
            }else{
                response.error(ctx, '修改失败')
            }
        }catch(err){
            console.log(err)
            response.error(ctx, err);
        }
    }
    async getCategoryInfo(ctx){
        console.log('info')
        try{
            const categoryData = await validate(ctx, getCategoryRules);
            const row = await categoryService.getCategoryById(categoryData.id);
            if(row){
                response.success(ctx, row)
            }else{
                response.error(ctx, '获取失败')
            }
        }catch(err){
            console.log(err)
            response.error(ctx, err);
        }
    }
    async getCategoryListByPage(ctx){
        console.log('a')
        const usp = new URLSearchParams(ctx.querystring);
        let page = Number(usp.get('page')) || 1;
        let limit = Number(usp.get('limit')) || 10;
        let keyword = usp.get('keyword') || '';
        try{
            const { rows, count } = await categoryService.getCategoryListByPage(page, limit, keyword);
            response.success(ctx, {
                page,
                limit,
                rows,
                total: count
            });
        }catch(err){
            console.log(err)
            response.error(ctx, err);
        }
    }
    async getAllCategorys(ctx){
        try{
            const rows = await categoryService.getAllCategorys();
            response.success(ctx, rows);
        }catch(err){
            console.log(err, 'err')
            response.error(ctx, err);
        }
    }
}


module.exports = new CategoryController();